package com.airsystem.pos.bengkel.controller.dialog;

import java.math.BigDecimal;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.component.TableFrame;
import com.airsystem.pos.bengkel.entity.DetailKendaraan;
import com.airsystem.pos.bengkel.entity.DetailLayanan;
import com.airsystem.pos.bengkel.entity.Kendaraan;
import com.airsystem.pos.bengkel.entity.Layanan;
import com.airsystem.pos.bengkel.interfaces.ILookup;
import com.airsystem.pos.bengkel.interfaces.IServices;
import com.airsystem.pos.bengkel.interfaces.ISystem;
import com.airsystem.pos.bengkel.model.dialog.MasterServiceDialogModel;
import com.airsystem.pos.bengkel.service.LayananServices;
import com.airsystem.pos.bengkel.util.Preconditions;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterServiceDialogController extends MasterServiceDialogModel implements ILookup, ISystem {
	private static final Logger LOG = Logger.getLogger(MasterServiceDialogController.class.getSimpleName());

	private IServices<DetailLayanan> service;

	public MasterServiceDialogController() {
		super();

		service = new LayananServices();
	}

	public void setup() {
		TableFrame.deleteTable(tableData, tableModel);
	}

	public void get() {
		String namaService = tableData.getValueAt(row, 0).toString();
		String merk = tableData.getValueAt(row, 1).toString();
		String subMerk = tableData.getValueAt(row, 2).toString();
		String harga = tableData.getValueAt(row, 3).toString().replace(".00", "");
		String serviceId = tableData.getValueAt(row, 4).toString();

		DetailLayanan dtLayanan = new DetailLayanan();
		dtLayanan.setId(Long.parseLong(serviceId));
		dtLayanan.setHarga(new BigDecimal(harga));

		DetailKendaraan dtKendaraan = new DetailKendaraan();
		dtKendaraan.setSub_merk(subMerk);

		Kendaraan kendaraan = new Kendaraan();
		kendaraan.setMerk(merk);
		dtKendaraan.setKendaraan(kendaraan);

		Layanan layanan = new Layanan();
		layanan.setNama(namaService);

		dtLayanan.setLayanan(layanan);
		dtLayanan.setDetail_kendaraan(dtKendaraan);

		setDetailLayanan(dtLayanan);
	}

	public void reset() {
		searchField.setText("");
		searchCombo.requestFocus();
	}

	public void category() {
		throw new UnsupportedOperationException("Not Implemented.");
	}

	public void search() {
		try {
			Preconditions.checkState(searchCombo.getSelectedIndex() == 0, "Combobox not chosen.");
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(this, "Silahkan pilih data!", "Pesan", JOptionPane.WARNING_MESSAGE);
			reset();

			return;
		}

		try {
			setup();
			Iterable<DetailLayanan> layananTypes = service.select(getSearchData());

			for (DetailLayanan item : layananTypes) {
				Vector<String> vector = new Vector<String>();
				vector.add(item.getLayanan().getNama());
				vector.add(item.getDetail_kendaraan().getKendaraan().getMerk());
				vector.add(item.getDetail_kendaraan().getSub_merk());
				vector.add(item.getHarga_s());
				vector.add(item.getId().toString());

				tableModel.addRow(vector);
			}

			tableData.setModel(tableModel);
		} catch (IllegalArgumentException e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Terdapat kesalahan pada input!", "Pesan", JOptionPane.WARNING_MESSAGE);
			reset();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void load() {
		try {
			setup();
			Iterable<DetailLayanan> layananTypes = service.select(new DetailLayanan());

			for (DetailLayanan item : layananTypes) {
				Vector<String> vector = new Vector<String>();
				vector.add(item.getLayanan().getNama());
				vector.add(item.getDetail_kendaraan().getKendaraan().getMerk());
				vector.add(item.getDetail_kendaraan().getSub_merk());
				vector.add(item.getHarga_s());
				vector.add(item.getId().toString());

				tableModel.addRow(vector);
			}
			tableData.setModel(tableModel);

			this.setVisible(true);
		} catch (Exception e) {
			this.setVisible(false);
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}
}